package com.power.services.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.group.TiGroupDAO;
import com.power.model.department.TiDepartment;
import com.power.model.group.TiGroup;
import com.power.model.hospital.TiHospital;
import com.power.util.Assent;
import com.power.util.PageUtil;

@Service("tiGroupService")
public class TiGroupServiceImpl implements TiGroupService {

	@Autowired
	private TiGroupDAO tiGroupDAO;

	public void create(TiGroup group) {
		this.tiGroupDAO.save(group);
	}

	public TiGroup read(Integer id) {
		return ((TiGroup) this.tiGroupDAO.load(id));
	}

    public void saveOrUpdate(TiGroup group) {
        tiGroupDAO.saveOrUpdate(group);
    }

    public void delete(String ids){
        String[] idAry = ids.split("_");
        for (int i = 0; i < idAry.length; i++) {
            Integer id = new Integer(idAry[i]);
            tiGroupDAO.deleteByKey(id);
        }
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> findAll(String groupName, int start, int limit, Integer hid,Integer did) {
        List<TiGroup> list = null;
        Map<String, Object> map = new HashMap<String, Object>();
        DetachedCriteria criteria = DetachedCriteria.forClass(TiGroup.class);
        if(!Assent.checkNullAndEmpty(groupName))
            criteria.add(Restrictions.like("name", "%"+groupName+"%"));
        criteria.add(Restrictions.eq("deleteFlag", "0"));
        criteria.add(Restrictions.eq("hid", hid));
        criteria.add(Restrictions.eq("did", did));
        list = tiGroupDAO.findByCriteria(criteria, start, limit);
        int count = tiGroupDAO.getRowCount(criteria);
        if(null == list)
            list = new ArrayList<TiGroup>();
        map.put("data", list);
        map.put("count", count);
        return map;
    }

    public String getProName(Integer code) {
    	String sql="SELECT a.name FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="+code;
    	List list = tiGroupDAO.findBySQL(sql);
    	String proName = "";
    	if(list.size()>0)
    		proName = list.get(0).toString();
    	return proName ;
    }
    
    public String getProId(Integer code) {
    	String sql="SELECT a.id FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="+code;
    	List list = tiGroupDAO.findBySQL(sql);
    	String proName = "";
    	if(list.size()>0)
    		proName = list.get(0).toString();
    	return proName ;
    }
    
    public String setDistrictName(Integer code) {
    	String sql="SELECT t.name FROM t_s_district t WHERE t.id = "+code;
    	List list = tiGroupDAO.findBySQL(sql);
    	String cityName = "";
    	if(list.size()>0)
    		cityName = list.get(0).toString();
    	return cityName ;
    }
    
    public void delete(TiGroup arg0) {
        tiGroupDAO.delete(arg0);
    }

    public void update(TiGroup arg0) {
        tiGroupDAO.update(arg0);
    }

	public void deleteHos(String ids) {
		String[] idAry = ids.split("_");
        for (int i = 0; i < idAry.length; i++) {
            Integer id = new Integer(idAry[i]);
            tiGroupDAO.executeSql("update t_i_group set deleteFlag=1 where id="+id);
        }
	}

	public List<TiGroup> findByDepartId(Integer did) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiGroup.class);
		criteria.add(Restrictions.eq("did", did));
		return tiGroupDAO.findByCriteria(criteria);
	}
    	public Map<String, Object> findComboxAll(String name, int start, int limit,
            Integer did) {
        List<TiGroup> list = null;
        Map<String, Object> map = new HashMap<String, Object>();
        DetachedCriteria criteria = DetachedCriteria
                .forClass(TiGroup.class);
        if (!Assent.checkNullAndEmpty(name))
            criteria.add(Restrictions.like("name", "%" + name + "%"));
        criteria.add(Restrictions.eq("deleteFlag", "0"));
        if (-1 != did)
            criteria.add(Restrictions.eq("did", did));
        list = tiGroupDAO.findByCriteria(criteria, start, limit);
        int count = tiGroupDAO.getRowCount(criteria);
        if (null == list)
            list = new ArrayList<TiGroup>();
        map.put("data", list);
        map.put("count", count);
        return map;
    }

		public PageUtil searchGroup(String keyword, int pageNo, int limit) {
			PageUtil pu = new PageUtil();
			DetachedCriteria criteria = DetachedCriteria.forClass(TiGroup.class);
			criteria.add(Restrictions.like("name", "%" + keyword + "%"));
			criteria.addOrder(Order.asc("firstChar"));
			List<TiGroup> list = tiGroupDAO.findByCriteria(criteria, (pageNo-1)*limit, limit);
			pu.setList(list);
			pu.setPageNo(pageNo);
			pu.setPageSize(limit);
			pu.setTotalRecords(tiGroupDAO.getRowCount(criteria));
			return pu;
		}

		public List<TiGroup> findBySearchKeyWord(String keyword) {
			DetachedCriteria criteria = DetachedCriteria.forClass(TiGroup.class);
			criteria.add(Restrictions.like("name", "%" + keyword + "%"));
			criteria.addOrder(Order.asc("firstChar"));
			List<TiGroup> list = tiGroupDAO.findByCriteria(criteria, 0, 10);
			return list;
		}

}